package com.xcity.game.conquests;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xcity.db.entity.Player;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.common.Cause;
import com.xcity.game.common.MessageConst;
import com.xcity.game.player.cache.PlayerCacheService;
import com.xcity.game.vip.VipUtils;
import com.xcity.pomelo.PomeloRoute;
import com.xcity.pomelo.request.ConquestTaxRequest;
import com.xcity.pomelo.request.ConquestsRequest;
import com.xcity.pomelo.response.ConquestTaxResponse;
import com.xcity.pomelo.response.ConquestsResponse;
import com.xcity.pomelo.response.HintResponse;
import com.xcity.util.SessionUtils;

import naga.x.App;
import naga.x.net.Session;
import naga.x.net.annotation.OPHandler;
import naga.x.net.impl.pomelo.annotation.PomeloOP;
import naga.x.service.Service;
import naga.x.service.ServiceException;

@OPHandler
public class ConquestsService implements Service {

	protected static final Logger LOG = LoggerFactory.getLogger(ConquestsService.class);
	
	@Override
	public void startup() throws ServiceException {
	}

	@Override
	public void shutdown() throws ServiceException {
	}

	@Override
	public String getId() {
		return getClass().getName();
	}
	
	@PomeloOP(PomeloRoute.CONQUESTS)
	protected void requestList(Session session, ConquestsRequest req) {
		Player player = session.getClient();
		List<Conquest> list = player.getConquests().getConquests();
		session.send(new ConquestsResponse(req.getSerial(), list, VipUtils.getTemplate(player.getVipLvl()).getCaptureBuildMaxCount()));
	}
	
	@PomeloOP(PomeloRoute.CONQUEST_TAX)
	protected void requestTax(Session session, ConquestTaxRequest req) {
		if (req.getPlayerId() == 0L || req.getBuildId() == 0) {
			session.send(new HintResponse(req.getSerial(), MessageConst.ILLEGAL_OPERATION));
			return;
		}
		Player player = session.getClient();
		Conquest conquest = player.getConquests().find(req.getPlayerId(), req.getBuildId());
		if (conquest == null) {
			session.send(new HintResponse(req.getSerial(), MessageConst.BUILD_NOT_OCCUPY));
			return;
		}
		if (SessionUtils.isOperateTooFrequent(session, SessionUtils.LAST_CONQUEST_TAX_TIME)) {
			session.send(new HintResponse(req.getSerial(), MessageConst.OPERATE_TOO_FREQUENT));
			return;
		}
		PlayerCacheService service = App.getApp().getServiceManager().get(PlayerCacheService.class);
		Player target = service.find(conquest.getOwnerId());
		int moneyGot = 0;
//		if (target != null) {
//			Build build = target.getBuildings().getBuild(conquest.getId());
//			int money;
//			if (build != null && (money = build.settle()) >= build.getMaxIncome()) {
//				build.resetIncome();
////				int half = money >> 1;
////				target.addGold(half, Cause.BUILD_CONQUEST_TAX);
//				player.addGold(moneyGot = money, Cause.BUILD_CONQUEST_TAX);
//				LOG.info("[CONQUEST MONEY]ID[{}]OWNER[{}]BUILD[{}]MONEY[{}]", player.getId(), target.getId(), build.getId(), money);
//			}
//		}
		if(target != null){
			BuildV2 b2 = target.getBuildings2().find(conquest.getId());
			if(b2 != null){
				int[] moneyInfo = conquest.calcMoney(b2.getTemplate().getProduceMoneyFactor(), BuildV2.MAX_TAKE_MONEY_SECOND);
				moneyGot = moneyInfo[0];
				if(moneyGot > 0){
					conquest.tryStartMakeMoney();
					player.addGold(moneyGot, Cause.BUILD_CONQUEST_TAX);
				}
				LOG.info("[CONQUEST MONEY]ID[{}]OWNER[{}]BUILD[{}]MONEY[{}]", player.getId(), target.getId(), b2.getId(), moneyGot);
			}
		}
		session.send(new ConquestTaxResponse(req.getSerial(), conquest, moneyGot));
	}

}
